package com.anyonavinfo.cpaddemo.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 * Created by ZuiC on 2017/2/4.
 */

public class WifiPwVerifyUtils {

    public String verifyPw(String newPw, String confirmPw) {
        if (newPw.isEmpty() || confirmPw.isEmpty()) {
            return "密码不能为空！";
        } else if (!(newPw.equals(confirmPw))) {
            return "密码不一致";
        } else if (newPw.length() != 8) {
            return "请输入8位密码！";
        } else if (!stringFilter(newPw).equals(newPw)) {
            return "密码中有非法字符！";
        } else {
            return null;
        }
    }

    private static String stringFilter(String str)throws PatternSyntaxException {
        // 只允许字母、数字和汉字
        String regEx = "[^a-zA-Z0-9\u0021-\u007E]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("").trim();
    }
}
